﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NewsPortal.Models;
using NewsPortal.Linq;
using NewsPortal.DataContracts;

namespace NewsPortal.Controllers
{
    public class CategoryController : Controller
    {
        //
        // GET: /Category/
        [OutputCache(CacheProfile = "MyCache", VaryByParam = "id")]
        public ActionResult Run(string id)
        {
            Guid newsId = new Guid(id);
            var portalModel = new PortalModel();
            using (var connection = DataContextFactory.Create())
            {
                // GetCategoryList
                var categories =
                    from category in connection.GetTable<Category>()
                    orderby category.Priority
                    select category;
                portalModel.AddCategores(categories.ToList());
                // GetCategoryName
                var curCategory =
                    from category in connection.GetTable<Category>()
                    where category.Id == newsId
                    select category;                    
                var newses =
                        from news in connection.GetTable<News>()
                        orderby news.Moment descending
                        where news.CategoryId == newsId
                        select news;
                portalModel.AddNews(curCategory.Take(1).ToArray()[0], newses.Take(10).ToArray());
            }
            return View("CategoryView", portalModel);
        }
        public  ActionResult Index()
        {
            return RedirectToRoute("Default");
        }
    }
}
